<?php
/**
 * 
 */
namespace app\home\controller\user;

use app\home\controller\Base;

class Handle extends Base
{
	private $params;

	public function __construct(){
		parent::__construct();
		// token验证
		if(false == Base::checkToken()){
			$this->error('token error!');
		}
		// 参数去token
		$params = input('post.');
		unset($params['token']);
		$this->params = $params;
	}

	public function bind_phone(){

		$params = $this->params;
		$memberInfo = $this->memberInfo;

		if($memberInfo['is_phone_checked']==1){
			// 原手机已验证过，则需要先验证原手机是否已经验证
			$phone_check_cookie = \app\common\lib\Cookie::get(HOME_PHONE_CHECK_COOKIE);
			if(false===$phone_check_cookie){
				// 未验证原手机号码
				$this->error('请先验证原手机号码');
			}else{
				if($phone_check_cookie['ischecked']==1){
					$this->bindPhone($params);
				}else{
					// 未验证原手机号码
					$this->error('请先验证原手机号码');
				}
			}
		}else{
			// 原手机未验证过，直接验证版定
			$this->bindPhone($params);
		}
	}

	private function bindPhone($params){
		$validate = $this->validate($params,'Phone.home_bind');
		if(true!==$validate){
			$this->error($validate);
		}
		$smscode = $params['smscode'];
		$phone = $params['phone'];

		// 修改用户手机号码
		$member_model = new \app\common\model\Member;
		$member_model -> where(['id'=>UID]) -> update(['phone'=>$phone,'is_phone_checked'=>1]);
		// 更新session
		$this->updateMemberSession();
		// 更改验证码数据库状态
		$sms_model = new \app\common\model\Sms;
		$sms_model -> where(['phone'=>$phone,'code'=>$smscode,'uid'=>UID]) -> update(['status'=>1]);
		$this->success('更新成功');
	}

	public function change_pwd(){
		$params = $this->params;
		$result = $this->validate($params, 'Member.home_pwd');
    	if(true !== $result) {
    	    $this->error($result);
        }
        $password = $params['password'];
        $smscode = $params['smscode'];
        $phone = $this->memberInfo['phone'];

		// 修改密码
		$memberModel = new \app\common\model\Member;
		$hash = hashPwd($password);
		$memberModel->where(['uid'=>UID])->update(['password'=>$hash]);
		// 更新session
		session('member.password',$hash);
        // 更改验证码数据库状态
		$sms_model = new \app\common\model\Sms;
		$sms_model -> where(['phone'=>$phone,'code'=>$smscode,'uid'=>UID]) -> update(['status'=>1]);
    	$this->success('修改成功');

	}

	public function change_safepwd(){
		$params = $this->params;
		$result = $this->validate($params, 'Member.home_safepwd');
    	if(true !== $result) {
    	    $this->error($result);
        }
        $smscode = $params['smscode'];
        $safe_password = $params['safe_password'];
		$phone = $this->memberInfo['phone'];

		// 修改密码
		$memberModel = new \app\common\model\Member;
		$hash = hashPwd($safe_password);
		$memberModel->where(['uid'=>UID])->update(['safe_password'=>$hash]);
		// 更新session
		session('member.safe_password',$hash);
		// 更改验证码数据库状态
		$sms_model = new \app\common\model\Sms;
		$sms_model -> where(['phone'=>$phone,'code'=>$smscode,'uid'=>UID]) -> update(['status'=>1]);
    	$this->success('修改成功');

	}

	public function mail_send(){
		$params = $this->params;

		$result = $this->validate($params, 'Mail.home_send');
    	if(true !== $result) {
    	    $this->error($result);
        }

		$target = $params['target'];
		$target_uid = model('member')->where(['mid'=>$target])->value('id');

		$title = $params['title'];
		$content = $params['content'];
		$post_time = $params['post_time'];

		$model = new \app\common\model\Mail;
		$data = [
			'target'=>$target_uid,
			'title'=>$title,
			'content'=>$content,
			'post_time'=>$post_time,
			'uid'=>UID,
		];
		$model->allowField(true)->save($data);
		$this->success('发送成功');

	}

	/**
	 * 原点升级
	 * @return [type] [description]
	 */
	public function upgrade(){
		$params = $this->params;

		$validate = $this->validate($params,'Upgrade');
    	if(true !== $validate) {
    	    $this->error($validate);
        }

        $smscode = $params['smscode'];
        $up_level_id = $params['up_level_id'];

        $uid = UID;
        $agent_id = $this->memberInfo['agent_id'];
        $level_id = $this->memberInfo['level_id'];

        $phone = $this->memberInfo['phone'];

        $model = model('member');
        $result = $model->upgrade($uid,$agent_id,$level_id,$up_level_id);
        if(true!==$result){
        	$this->error($model->getError());
        }else{
        	$this->updateMemberSession();
        	// 更新等级缓存
	        $CACHE = \app\common\cache\CacheData::getInstance('Level');
	        $CACHE -> updateCache();
        	// 更改验证码数据库状态
			$sms_model = new \app\common\model\Sms;
			$sms_model -> where(['phone'=>$phone,'code'=>$smscode,'uid'=>UID]) -> update(['status'=>1]);
        	$this->success('升级成功');
        }
	}

}